package com.xtool.livewallpaper.video

import android.media.MediaPlayer
import android.net.Uri
import android.util.Log
import android.view.Surface
import android.view.SurfaceHolder
import com.xtool.livewallpaper.base.MainApplication


class SystemMediaPlayer : MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
    MediaPlayer.OnCompletionListener, MediaPlayer.OnVideoSizeChangedListener {
    companion object {
        const val TAG = "SystemMediaPlayer"
    }

    private val mediaPlayer: MediaPlayer by lazy { MediaPlayer() }
    private var isPrepared: Boolean = false
    private var isLooping = true
    private var isImmediate = true
    private var progress = 0
    private var isMute = true
    private var prepareCallback = {}

    init {
        mediaPlayer.setScreenOnWhilePlaying(false)
        mediaPlayer.setOnPreparedListener(this)
        mediaPlayer.setOnErrorListener(this)
        mediaPlayer.setOnCompletionListener(this)
    }

    fun play(path: Uri, progress: Int = 0, immediate: Boolean = true) {
        Log.e("22222222", "play")
        this.progress = progress
        isImmediate = immediate
        mediaPlayer.setDataSource(MainApplication.gContext, path)
        mediaPlayer.prepareAsync()
        mediaPlayer.isLooping = isLooping
    }

    fun setOnPreparedCallback(callback: () -> Unit) {
        this.prepareCallback = callback
    }

    fun start() {
        if (isPrepared) {
            if (!isPlaying()) {
                Log.e("22222222", "start")
                mediaPlayer.start()
            }
        }
    }

    fun pause() {
        try {
            if (isPlaying()) {
                mediaPlayer.pause()
            }
        } catch (e: Exception) {
            Log.e(TAG, e.message ?: "")
        }
    }

    fun seekTo(progress: Int) {
        if (isPrepared) {
            mediaPlayer.seekTo(progress)
            this.progress = progress
        } else {
            this.progress = progress
        }
    }

    fun setDisplay(surfaceHolder: SurfaceHolder) {
        setSurface(surfaceHolder.surface)
    }

    fun setSurface(surface: Surface) {
        try {
            mediaPlayer.setSurface(surface)
        } catch (e: Exception) {
            Log.e(TAG, e.message ?: "")
        }
    }

    fun stop() {
        if (isPrepared) {
            progress = 0
            mediaPlayer.stop()
        }

    }

    fun release() {
        progress = 0
        if (isPrepared && isPlaying()) {
            mediaPlayer.stop()
        }
        isPrepared = false
        mediaPlayer.release()
    }

    fun setLoop(isLooping: Boolean) {
        this.isLooping = isLooping
    }

    fun setMute(isMute: Boolean) {
        if (isPrepared) {
            if (isMute) {
                mediaPlayer.setVolume(0f, 0f)
            } else {
                mediaPlayer.setVolume(1f, 1f)
            }
        }
        this.isMute = isMute
    }

    fun isPlaying(): Boolean {
        return mediaPlayer.isPlaying
    }

    fun getCurrentPosition(): Int {
        return if (isPrepared) mediaPlayer.currentPosition else 0
    }


    override fun onPrepared(mp: MediaPlayer?) {
        Log.e("22222222", "onPrepared")
        isPrepared = true
        seekTo(progress)
        setMute(isMute)
        if (isImmediate) {
            start()
        }
        prepareCallback()
    }

    override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean {
        isPrepared = false
        return false
    }

    override fun onCompletion(mp: MediaPlayer?) {
    }


    override fun onVideoSizeChanged(mp: MediaPlayer?, width: Int, height: Int) {
    }
}